import { defineStore } from 'pinia'
import { ref } from 'vue'
//用户模块token setToken removeToken
export const useUserStore = defineStore(
  'user',
  () => {
    // token是令牌
    const token = ref('')
    const userInfo = ref({})
    const userName = ref('')
    const book = ref('')
    const clickBook = ref('')
    const setToken = (newToken) => {
      token.value = newToken
    }
    const removeToken = () => {
      token.value = ''
    }
    const setUserInfo = (newObj) => {
      userInfo.value = newObj
    }
    const setUserName = (newName) => {
      userName.value = newName
    }
    const setBook = (newBook) => {
      console.log('传参数前book：')
      console.log(book.value)
      book.value = newBook
      console.log('传参后book：')
      console.log(book.value)
    }
    const clear = () => {
      token.value = ''
      userInfo.value = {}
      userName.value = ''
      book.value = ''
      clickBook.value = ''
    }
    return {
      token,
      userInfo,
      userName,
      setToken,
      removeToken,
      setUserInfo,
      setUserName,
      book,
      setBook,
      clickBook,
      clear
    }
  },
  {
    persist: {
      enabled: true, //Store中数据持久化生效
      storage: localStorage
    }
  }
)
